package com.manage.project.biz.service.impl;

import java.util.Date;
import java.util.List;

import com.manage.project.biz.domain.CivilEnginneeringAllMaintenance;
import com.manage.project.biz.domain.CivilEnginneeringTechnicalTotal;
import com.manage.project.biz.mapper.CivilEnginneeringTechnicalTotalMapper;
import com.manage.project.biz.service.ICivilEnginneeringAllMaintenanceService;
import com.manage.project.biz.service.ICivilEnginneeringRegularCheckService;
import com.manage.project.biz.service.ICivilEnginneeringTechnicalTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * 土建结构技术状况评定Service业务层处理
 *
 * @author 小赵
 * @date 2022-05-12
 */
@Service
public class CivilEnginneeringTechnicalTotalServiceImpl implements ICivilEnginneeringTechnicalTotalService
{
    @Autowired
    private CivilEnginneeringTechnicalTotalMapper civilEnginneeringTechnicalTotalMapper;
    @Autowired
    private ICivilEnginneeringRegularCheckService civilEnginneeringRegularCheckService;
    @Autowired
    private ICivilEnginneeringAllMaintenanceService maintenanceService;

    /*根据隧道名称和检查日期查询土建结构的检查记录*/
    @Override
    public CivilEnginneeringTechnicalTotal selectByTunnelNameAndDate(String tunnelName, Date adjustData) {
        return civilEnginneeringTechnicalTotalMapper.getByTunnelNameAndDate(tunnelName,adjustData);
    }

    /**
     * 查询土建结构技术状况评定
     *
     * @param technologyId 土建结构技术状况评定主键
     * @return 土建结构技术状况评定
     */
    @Override
    public CivilEnginneeringTechnicalTotal selectCivilEnginneeringTechnicalTotalByTechnologyId(Long technologyId)
    {
        return civilEnginneeringTechnicalTotalMapper.selectCivilEnginneeringTechnicalTotalByTechnologyId(technologyId);
    }

    /**
     * 查询土建结构技术状况评定列表
     *
     * @param civilEnginneeringTechnicalTotal 土建结构技术状况评定
     * @return 土建结构技术状况评定
     */
    @Override
    public List<CivilEnginneeringTechnicalTotal> selectCivilEnginneeringTechnicalTotalList(CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal)
    {
        return civilEnginneeringTechnicalTotalMapper.selectCivilEnginneeringTechnicalTotalList(civilEnginneeringTechnicalTotal);
    }

    /**
     * 新增土建结构技术状况评定
     *
     * @param civilEnginneeringTechnicalTotal 土建结构技术状况评定
     * @return 结果
     */
    @Override
    public int insertCivilEnginneeringTechnicalTotal(CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal) {
        String name1 = "装饰物";
        String name2 = "吊顶及预埋件";
        String name3 = "洞门";
        String name4 = "洞口";
        String name5 = "衬砌";
        String name6 = "路面";
        String name7 = "检修道";
        String name8 = "排水设施";
        String name9 = "交通标志/标线";
        String pclass = "";//评定等级
        String suggest = "";//养护措施建议
        int direct = civilEnginneeringTechnicalTotal.getDirectLimit();//装饰物权重
        int top = civilEnginneeringTechnicalTotal.getTopLimit();//吊顶权重
        int door = civilEnginneeringTechnicalTotal.getTunnelDoorLimit();//洞门权重
        int hole = civilEnginneeringTechnicalTotal.getTunnelHoleLimit();//洞口权重
        int lining = civilEnginneeringTechnicalTotal.getLinningLimit();//衬砌权重
        int roadsurface = civilEnginneeringTechnicalTotal.getRoadsurfaceLimit();//路面权重
        int jianxiudao = civilEnginneeringTechnicalTotal.getJianxiudaoLimit();//检修道权重
        int water = civilEnginneeringTechnicalTotal.getWaterLimit();//排水设施权重
        int marking = civilEnginneeringTechnicalTotal.getMarkingLimit();//交通标志或标线权重

        //获取tunnelName参数
        String tunnelName = civilEnginneeringTechnicalTotal.getTunnelName();
        Date inspectionDate=civilEnginneeringTechnicalTotal.getAdjustData();
        //获取各土建结构分项设施的最大值
        int directMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name1,inspectionDate);
        int topMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name2,inspectionDate);
        int doorMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name3,inspectionDate);
        int holeMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name4,inspectionDate);
        int liningMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name5,inspectionDate);
        int roadsurfaceMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name6,inspectionDate);
        int jianxiudaoMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name7,inspectionDate);
        int waterMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name8,inspectionDate);
        int markingMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name9,inspectionDate);

        //计算土建结构技术状况评分JGCI
        double sum = directMax * direct + topMax * top + doorMax * door + holeMax * hole + liningMax * lining
                +
                roadsurfaceMax * roadsurface + jianxiudaoMax * jianxiudao + waterMax * water + markingMax * marking;
        double total=sum/4;
        double jgci =  100- total;
        int JGCI = (int) jgci;
        if (JGCI >= 85) {
            pclass = "一类";
            suggest = "正常养护";
        } else if (JGCI < 85 && JGCI >= 70) {
            pclass = "二类";
            suggest = "应对结构破损部位进行监测或检查，必要时实施保养维修;机电设施进行正常养护，应对关键设备及时修复";
        } else if (JGCI < 70 && JGCI >= 55) {
            pclass = "三类";
            suggest = "应对结构破损部位进行重点监测,并对局部实施保养维修;机电设施需进行专项工程";
        } else if (JGCI < 55 && JGCI >= 40) {
            pclass = "四类";
            suggest = "应尽快实施结构病害处治措施;对机电设施应进行专项工程，并应及时实施交通管制";
        } else if (JGCI < 40) {
            pclass = "五类";
            suggest = "应及时关闭隧道，实施病害处治,特殊情况需进行局部重建或改建";
        }
        civilEnginneeringTechnicalTotal.setGrade(JGCI);
        civilEnginneeringTechnicalTotal.setPclass(pclass);
        civilEnginneeringTechnicalTotal.setDirectMax(directMax);
        civilEnginneeringTechnicalTotal.setJianxiudaoMax(jianxiudaoMax);
        civilEnginneeringTechnicalTotal.setLiningMax(liningMax);
        civilEnginneeringTechnicalTotal.setMarkingMax(markingMax);
        civilEnginneeringTechnicalTotal.setTopMax(topMax);
        civilEnginneeringTechnicalTotal.setTunnelDoorMax(doorMax);
        civilEnginneeringTechnicalTotal.setWaterMax(waterMax);
        civilEnginneeringTechnicalTotal.setRoadsurfaceMax(roadsurfaceMax);
        civilEnginneeringTechnicalTotal.setTunnelHoleMax(holeMax);
        civilEnginneeringTechnicalTotal.setSuggest(suggest);

        CivilEnginneeringAllMaintenance maintenance= new CivilEnginneeringAllMaintenance();
        maintenance.setTunnelId(civilEnginneeringTechnicalTotal.getTunnelId());
        maintenance.setRoadName(civilEnginneeringTechnicalTotal.getRoadName());
        maintenance.setTunnelName(civilEnginneeringTechnicalTotal.getTunnelName());
        maintenance.setMaintenanceDate(new Date());
        maintenance.setTotalGrade(pclass);
        maintenance.setMaintenanceScheme(suggest);
        maintenance.setMaintenanceCondition("未养护");
        maintenanceService.insertCivilEnginneeringAllMaintenance(maintenance);

        return civilEnginneeringTechnicalTotalMapper.insertCivilEnginneeringTechnicalTotal(civilEnginneeringTechnicalTotal);
    }


    /**
     * 修改土建结构技术状况评定
     *
     * @param civilEnginneeringTechnicalTotal 土建结构技术状况评定
     * @return 结果
     */
    @Override
    public int updateCivilEnginneeringTechnicalTotal(CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal)
    {
        String name1 = "装饰物";
        String name2 = "吊顶及预埋件";
        String name3 = "洞门";
        String name4 = "洞口";
        String name5 = "衬砌";
        String name6 = "路面";
        String name7 = "检修道";
        String name8 = "排水设施";
        String name9 = "交通标志/标线";
        String pclass = "";//评定等级
        String suggest = "";//养护措施建议
        int direct = civilEnginneeringTechnicalTotal.getDirectLimit();//装饰物权重
        int top = civilEnginneeringTechnicalTotal.getTopLimit();//吊顶权重
        int door = civilEnginneeringTechnicalTotal.getTunnelDoorLimit();//洞门权重
        int hole = civilEnginneeringTechnicalTotal.getTunnelHoleLimit();//洞口权重
        int lining = civilEnginneeringTechnicalTotal.getLinningLimit();//衬砌权重
        int roadsurface = civilEnginneeringTechnicalTotal.getRoadsurfaceLimit();//路面权重
        int jianxiudao = civilEnginneeringTechnicalTotal.getJianxiudaoLimit();//检修道权重
        int water = civilEnginneeringTechnicalTotal.getWaterLimit();//排水设施权重
        int marking = civilEnginneeringTechnicalTotal.getMarkingLimit();//交通标志或标线权重

        //获取tunnelName参数
        String tunnelName = civilEnginneeringTechnicalTotal.getTunnelName();
        Date inspectionDate=civilEnginneeringTechnicalTotal.getAdjustData();
        //获取各土建结构分项设施的最大值
        int directMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name1,inspectionDate);
        int topMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name2,inspectionDate);
        int doorMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name3,inspectionDate);
        int holeMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name4,inspectionDate);
        int liningMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name5,inspectionDate);
        int roadsurfaceMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name6,inspectionDate);
        int jianxiudaoMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name7,inspectionDate);
        int waterMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name8,inspectionDate);
        int markingMax=civilEnginneeringRegularCheckService.getMaxByTunnelNameAndFacilityName(tunnelName,name9,inspectionDate);

        //计算土建结构技术状况评分JGCI
        double sum = directMax * direct + topMax * top + doorMax * door + holeMax * hole + liningMax * lining
                +
                roadsurfaceMax * roadsurface + jianxiudaoMax * jianxiudao + waterMax * water + markingMax * marking;
        double total=sum/4;
        double jgci =  100- total;
        int JGCI = (int) jgci;
        if (JGCI >= 85) {
            pclass = "一类";
            suggest = "正常养护";
        } else if (JGCI < 85 && JGCI >= 70) {
            pclass = "二类";
            suggest = "应对结构破损部位进行监测或检查，必要时实施保养维修;机电设施进行正常养护，应对关键设备及时修复";
        } else if (JGCI < 70 && JGCI >= 55) {
            pclass = "三类";
            suggest = "应对结构破损部位进行重点监测,并对局部实施保养维修;机电设施需进行专项工程";
        } else if (JGCI < 55 && JGCI >= 40) {
            pclass = "四类";
            suggest = "应尽快实施结构病害处治措施;对机电设施应进行专项工程，并应及时实施交通管制";
        } else if (JGCI < 40) {
            pclass = "五类";
            suggest = "应及时关闭隧道，实施病害处治,特殊情况需进行局部重建或改建";
        }
        civilEnginneeringTechnicalTotal.setGrade(JGCI);
        civilEnginneeringTechnicalTotal.setPclass(pclass);
        civilEnginneeringTechnicalTotal.setDirectMax(directMax);
        civilEnginneeringTechnicalTotal.setJianxiudaoMax(jianxiudaoMax);
        civilEnginneeringTechnicalTotal.setLiningMax(liningMax);
        civilEnginneeringTechnicalTotal.setMarkingMax(markingMax);
        civilEnginneeringTechnicalTotal.setTopMax(topMax);
        civilEnginneeringTechnicalTotal.setTunnelDoorMax(doorMax);
        civilEnginneeringTechnicalTotal.setWaterMax(waterMax);
        civilEnginneeringTechnicalTotal.setRoadsurfaceMax(roadsurfaceMax);
        civilEnginneeringTechnicalTotal.setTunnelHoleMax(holeMax);
        civilEnginneeringTechnicalTotal.setSuggest(suggest);
        return civilEnginneeringTechnicalTotalMapper.updateCivilEnginneeringTechnicalTotal(civilEnginneeringTechnicalTotal);
    }

    /**
     * 批量删除土建结构技术状况评定
     *
     * @param technologyIds 需要删除的土建结构技术状况评定主键
     * @return 结果
     */
    @Override
    public int deleteCivilEnginneeringTechnicalTotalByTechnologyIds(Long[] technologyIds)
    {
        return civilEnginneeringTechnicalTotalMapper.deleteCivilEnginneeringTechnicalTotalByTechnologyIds(technologyIds);
    }

    /**
     * 删除土建结构技术状况评定信息
     *
     * @param technologyId 土建结构技术状况评定主键
     * @return 结果
     */
    @Override
    public int deleteCivilEnginneeringTechnicalTotalByTechnologyId(Long technologyId)
    {
        return civilEnginneeringTechnicalTotalMapper.deleteCivilEnginneeringTechnicalTotalByTechnologyId(technologyId);
    }
}
